package com.foreveross.action;

import java.util.List;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.log4j.Logger;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

import com.foreveross.common.CommonUtil;
import com.foreveross.common.Constants;
import com.foreveross.service.LogService;
import com.foreveross.service.OrderInfoService;
import com.foreveross.util.AuthUtil;
import com.foreveross.util.ExcelUtil;
import com.foreveross.util.JSONUtil;
import com.foreveross.vo.OrderInfo;
import com.foreveross.vo.common.PageVo;
import com.foreveross.vo.common.ResultVo;

@Controller
@RequestMapping(value = "/SysOrderInfoController")
public class SysOrderInfoController{


	private Logger log = Logger.getLogger(SysOrderInfoController.class);

	@Resource
	OrderInfoService orderInfoService;

	@Resource
	private LogService logService;
	
	
	@RequestMapping(method = RequestMethod.POST, value = "/getOrderInfoList")
	public void getOrderInfoList(HttpServletRequest request,
			HttpServletResponse response,String userId,String houseId,String businessName,
			String tenementId,String payType,String payStatus,String startOrderTime,
			String endOrderTime,String resCode,
			String page,String pageSize) {
		ResultVo resultVo = new ResultVo();
		try {
			int total = (int)orderInfoService.getOrderInfoListCount(null,userId,houseId,tenementId,businessName,
					payType,payStatus,startOrderTime,endOrderTime,resCode);
			PageVo<OrderInfo> pageVo = new PageVo<OrderInfo>(page,total,pageSize);
			List<OrderInfo> list = orderInfoService.getOrderInfoList(null,userId,houseId,tenementId,businessName,
					payType,payStatus,startOrderTime,endOrderTime,resCode,pageVo.getpageIndex(),
					pageVo.getpageSize());
			pageVo.setRows(list);
			resultVo.setSuccess(true);
			resultVo.setData(pageVo);
			log.info("订单 查看成功!");
		} catch (Exception e) {
			resultVo.setSuccess(false);
			resultVo.setMessage("订单 查看失败");
			log.error("订单 查看失败!", e);
		}
		CommonUtil.PrintWriterResultVoWithJSON(response, resultVo);
	}
	
	@RequestMapping(method = RequestMethod.POST, value = "/exportOrderInfoListExcel")
	public void exportOrderInfoListExcel(HttpServletRequest request,
			HttpServletResponse response,String userId,String houseId,String businessName,
			String tenementId,String payType,String payStatus,String startOrderTime,
			String endOrderTime,String resCode) {
		try {
			List<OrderInfo> list = orderInfoService.getOrderInfoList(null,userId,houseId,tenementId,businessName,
					payType,payStatus,startOrderTime,endOrderTime,resCode,0,Constants.PAGE_MAX_SIZE);
			String fileName= "润U汇-订单管理";
			String [] titles ={
					"买家姓名", "商品名", "商铺名", "楼盘名","房号", "原价(元)", 
					"现价(元 )", "购买数量", "账单时间", "支付方式", "支付状态"
			};
			String [] valueName ={
					"houseUser-name", "hrFavorable-name", "hrBusiness-name", "hrHouses-name", "houseUser-resCode","hrFavorable-price", 
					"hrFavorable-favorablePrice", "count", "orderTime", "payTypeName", "payStatusName"
			};
			ExcelUtil<OrderInfo> excelUtil = new ExcelUtil<OrderInfo>(fileName, titles,valueName, list);
			excelUtil.expExcel(response);
			log.info("订单 EXCEL 导出成功!");
		} catch (Exception e) {
			log.error("订单 EXCEL 导出失败!", e);
		}
	}
	
	
	@RequestMapping(method = RequestMethod.POST, value = "/getOrderInfoById")
	public void getOrderInfoById(HttpServletRequest request,
			HttpServletResponse response,String id) {
		ResultVo resultVo = new ResultVo();
		try {
			OrderInfo OrderInfo = orderInfoService.getOrderInfoById(id);
			resultVo.setSuccess(true);
			resultVo.setData(OrderInfo);
			log.info("订单 查看成功!");
		} catch (Exception e) {
			resultVo.setSuccess(false);
			resultVo.setMessage("订单  查看失败");
			log.error("订单 查看失败!", e);
		}
		CommonUtil.PrintWriterResultVoWithJSON(response, resultVo);
	}

	
	/**
	 * 线下付款状态处理
	 * @param request
	 * @param response
	 * @param id
	 */
	@RequestMapping(method = RequestMethod.POST, value = "/outlineOrderInfoById")
	public void outlineOrderInfoById(HttpServletRequest request,
			HttpServletResponse response,String id ,int payStatus) {
		ResultVo resultVo = new ResultVo();
		try {
			List<OrderInfo> orderInfoList = orderInfoService.offLineOrderInfo(id, payStatus);
			resultVo.setSuccess(true);
			resultVo.setData(orderInfoList);
			log.info("订单状态修改成功!");
		} catch (Exception e) {
			resultVo.setSuccess(false);
			resultVo.setMessage("订单状态修改成功失败");
			log.error("订单状态修改成功失败!", e);
		}
		CommonUtil.PrintWriterResultVoWithJSON(response, resultVo);
	}
	
//	@RequestMapping(method = RequestMethod.POST, value = "/addOrUpdateHrFavorable")
//	public void addOrUpdateHrFavorable(HttpServletRequest request,
//			HttpServletResponse response, OrderInfo OrderInfo,
//			String updateType) {
//		ResultVo resultVo = new ResultVo();
//		try {
//			orderInfoService.saveOrUpdate(OrderInfo, updateType);
//			resultVo.setSuccess(true);
//			logService.saveLog(
//					CommonUtil.getAddrIp(request),
//					Constants.OPERATE_TYPE_BACKGROUND,
//					updateType.equals(Constants.UPDATE_ADD) ? Constants.LOG_ADD
//							: Constants.LOG_UPDATE,
//					"订单" + Constants.getUpdateType(updateType),
//					AuthUtil.getUserid(request),
//					Constants.getUpdateType(updateType) + "订单["
//							+ OrderInfo.toString() + "]");
//			log.info("订单 " + updateType + "成功!");
//		} catch (Exception e) {
//			resultVo.setSuccess(false);
//			resultVo.setMessage("订单 " + updateType + "失败");
//			log.error("订单" + updateType + "失败!", e);
//		}
//		
//		CommonUtil.PrintWriterResultVoWithJSON(response, resultVo);
//	}
//	
//	
//
//	/**
//	 * 删除润优惠
//	 * 
//	 * @param request
//	 * @param response
//	 * @param ids
//	 */
//	@RequestMapping(method = RequestMethod.POST, value = "/deleteHrFavorable")
//	public void deleteHrFavorable(HttpServletRequest request,
//			HttpServletResponse response, String ids) {
//		ResultVo resultVo = new ResultVo();
//		try {
//			orderInfoService.deleteObjecList("OrderInfo", ids);
//			resultVo.setSuccess(true);
//			logService.saveLog(CommonUtil.getAddrIp(request),
//					Constants.OPERATE_TYPE_BACKGROUND, Constants.LOG_DEL,
//					"删除惠优选", AuthUtil.getUserid(request), "润优惠ID[" + ids + "]");
//			log.info("润优惠删除成功!");
//		} catch (Exception e) {
//			resultVo.setSuccess(false);
//			resultVo.setMessage("润优惠删除失败");
//			log.error("润优惠删除失败!", e);
//		}
//		
//		CommonUtil.PrintWriterResultVoWithJSON(response, resultVo);
//	}

}
